﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Http;
using System.Web;
using System.Diagnostics;
using System.Threading.Tasks;



namespace 调用BH窗体
{
    public partial class from1 : Form
    {
        public from1()
        {
            InitializeComponent();
        }
        // private Uri url = new Uri("http://localhost:8001/api/ipaddress");


        /// <summary>
        /// 启动BH主进程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_startBHPro_Click(object sender, EventArgs e)
        {
            //判断进程是否启动
            bool b = false;
            System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processList)
            {
                if (process.ProcessName.ToUpper() == "ZLBH")
                {
                    b = true;
                }
            }
            if (!b)
            {
                string url = Txt_BHClientPath.Text + "\\ZLBH.exe";
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = url;
                string path = Txt_BHClientPath.Text;
                psi.WorkingDirectory = path;
                psi.Arguments = Txt_BHUserName.Text + " " + Txt_BHPassword.Text + " " + " 运行 8982D9B6-FD40-4F9F-ABDF-F1DFEC232157";
                Process.Start(psi);
                MessageBox.Show("启动BH主进程");
            }
        }
        /// <summary>
        /// 启动BH主进程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        private void btn_OpenBHWindow_Click(object sender, EventArgs e)
        {
            bool b = false;
            System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processList)
            {
                if (process.ProcessName.ToUpper() == "ZLBH")
                {
                    b = true;
                }
            }
            if (b)
            {
                string loginname = Txt_BHUserName.Text;
                // string loginurl = Txt_BHRUL.Text + "@Open?smartformloadtype=" + txt_OpenStyle.Text;
                string loginurl = Txt_BHRUL.Text;
                string loginname_utf8 = System.Web.HttpUtility.UrlDecode(loginname, System.Text.Encoding.UTF8);
                string loginurl_utf8 = System.Web.HttpUtility.UrlDecode(loginurl, System.Text.Encoding.UTF8);
                string url = "callzlbh:\\\\" + loginname_utf8 + "|" + loginurl_utf8;
                System.Diagnostics.Process.Start(url);
            }
            else
            {
                MessageBox.Show("请先启动BH主进程！");
            }
        }

        /// <summary>
        /// 启用BH并用BH窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_openBH_Click(object sender, EventArgs e)
        {
            bool b = false;
            System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processList)
            {
                if (process.ProcessName.ToUpper() == "ZLBH")
                {
                    b = true;
                }
            }
            if (!b)
            {
                string url = Txt_BHClientPath.Text + "\\ZLBH.exe";
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = url;
                string path = Txt_BHClientPath.Text;
                psi.WorkingDirectory = path;
                psi.Arguments = Txt_BHUserName.Text + " " + Txt_BHPassword.Text + " " + " 运行 8982D9B6-FD40-4F9F-ABDF-F1DFEC232157";
                Process.Start(psi);
                
            }
            Task.Delay(10000);//延迟10秒执行，可能主进程没有启动完成
            string loginname = Txt_BHUserName.Text;
           // string loginurl = Txt_BHRUL.Text + "@Open?smartformloadtype=" + txt_OpenStyle.Text;
            string loginurl = Txt_BHUserName.Text;
            string loginname_utf8 = System.Web.HttpUtility.UrlDecode(loginname, System.Text.Encoding.UTF8);
            string loginurl_utf8 = System.Web.HttpUtility.UrlDecode(loginurl, System.Text.Encoding.UTF8);
            string bhurl = "callzlbh:\\\\" + loginname_utf8 + "|" + loginurl_utf8;
            System.Diagnostics.Process.Start(bhurl);

        }

        private void btn_CloseBH_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processList)
            {
                if (process.ProcessName.ToUpper() == "ZLBH")
                {
                    process.Kill(); //结束进程
                }
            }
        }


    }
}
